/*
 * Name        : AppMetaData.java
 * Classes     : AppMetaData
 * Version     : 1.0
 * Date        : 2013-08-29
 *
 * Copyright 2013 CMCC.  All rights reserved.
 * This material, including documentation and any related computer
 * programs, is protected by copyright controlled by CMCC.  All
 * rights are reserved.  Copying, including reproducing, storing,
 * adapting or translating, any or all of this material requires the
 * prior written consent of CMCC.  This material also contains
 * confidential information which may not be disclosed to others
 * without the prior written consent of CMCC.
 */

package com.xgkj.diyiketang.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.xgkj.diyiketang.BuildConfig;


/**
 * class name AppMetaData.java
 * author renhaibin
 * date 2014-11-24 下午2:48:51
 */
public class AppMetaData {


    /**
     * The base url value, which is used to combine full url. Default is public server url.
     */
//     public static String HOST_BASE_URL = "http://192.168.1.108:8097/classroom/";
//    上线环境
    public static String HOST_BASE_URL = "http://app.sjmingze.com:8096/classroom/";
//    测试后台环境
//    public static String HOST_BASE_URL = "http://app.sjmingze.com:8099/classroom/";
    //    测试后台环境（二次更改，本地服务器）
//    public static String HOST_BASE_URL = "http://192.168.0.142:8097/classroom/";
    /**
     * The log level, default is print release log only
     */
    public static int LOG_LEVEL = 0;


    /**
     * The key name for HOST_BASE_URL
     */
    public static final String META_DATA_NAME_BASE_URL = "HOST_BASE_URL";


    /**
     * The key name for meta_data_name_log_level
     */
    public static final String META_DATA_NAME_LOG_LEVEL = "LOG_LEVEL";

    /**
     * The key name for META_DATA_NAME_SIGNATURE_TYPE
     */
    public static final String META_DATA_NAME_SIGNATURE_TYPE = "SIGNATURE_TYPE";

    /**
     * Init meta data from app Manifest
     *
     * @param context The context used to read meta data
     */
    public static void init(Context context) {
        String baseUrl = getMetaValue(context, META_DATA_NAME_BASE_URL);
        if (baseUrl != null) {
            HOST_BASE_URL = baseUrl;
        }

        getDebugLog(context);


//        String logLevel = getMetaValue(context, META_DATA_NAME_LOG_LEVEL);
//        if (logLevel != null) {
//            try {
//                LOG_LEVEL = Integer.parseInt(logLevel);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }

    }

    private static void getDebugLog(Context context) {
//		String str = getMetaValue(context, "DISABLE_LOG");
//		if (str != null && str.length() > 2) {
//			PalLog.DEBUG = !Boolean.valueOf(str);
//            Log.e("buildConfig.debug","Debug:"+BuildConfig.DEBUG);
//		}

//        PalLog.DEBUG = true;
        PalLog.DEBUG = BuildConfig.DEBUG;
    }

    public static String getMetaValue(Context context, String key) {
        try {
            PackageManager packageManager = context.getPackageManager();
            if (packageManager != null) {
                ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(),
                        PackageManager.GET_META_DATA);

                if (appInfo.metaData != null) {
                    Object value = appInfo.metaData.get(key);
                    if (value != null) {
                        return value.toString();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
